#!/bin/sh

# Usage: ./release NEW_VERSION [ dev | SINCE_COMMITISH ]
# 
# If "dev" is supplied as the second argument, a development snapshot
# is done rather than a real release, i.e.:
#    * the --snapshot --auto options are passed to gbp-dch
#    * no commit or tag is created
# else, the second argument is passed to gbp-dch's --since option.

### source the configuration files

. config/amnesia
if [ -e config/amnesia.local ] ; then
   . config/amnesia.local
fi

### init variables

NEW_VERSION="$1"
if [ "$2" = dev ]; then
   SNAPSHOT=yes
else
   SNAPSHOT=no
   SINCE="$2"
fi

### helper functions

fatal () {
   echo "Fatal: $@" >&2
   exit 2
}

### sanity checks

[ -n "${NEW_VERSION}" ] \
   || fatal "the new version must be supplied on the command-line."
[ -n "${AMNESIA_DEV_FULLNAME}" ] \
   || fatal "AMNESIA_DEV_FULLNAME must be set in config/amnesia"
[ -n "${AMNESIA_DEV_EMAIL}" ] \
   || fatal "AMNESIA_DEV_EMAIL must be set in config/amnesia"
[ -n "${AMNESIA_DEV_KEYID}" ] \
   || fatal "AMNESIA_DEV_KEYID must be set in config/amnesia"
[ -x "`which git`" ] \
   || fatal "could not find git, please apt-get install git-core"
[ -x "`which gbp`" ] \
   || fatal "could not find gbp, please apt-get install git-buildpackage"

### main

export DEBFULLNAME="${AMNESIA_DEV_FULLNAME}"
export DEBEMAIL="${AMNESIA_DEV_EMAIL}"

# update the Changelog
echo "Updating debian/changelog from Git history..."
gbp dch \
   `if [ ${SNAPSHOT} = yes ]; then echo '--snapshot --auto' ; fi` \
   `if [ ${SNAPSHOT} = no -a -n ${SINCE} ]; then echo "--since=${SINCE}" ; fi` \
   `if [ ${SNAPSHOT} = no -a -z ${SINCE} ]; then echo "--auto" ; fi` \
   --new-version="${NEW_VERSION}" \
   --ignore-branch \
   -- '*' ':!wiki' \
   || fatal "gbp dch failed."

# cleanup some parts of the changelog
perl -pi'' -e 's/\A  \[ IkiWiki::Plugin::po::change \]\n//' debian/changelog
perl -pi'' -e 's/\A  \* update[d]? PO file[s]?[.]?\n//' debian/changelog
perl -pi'' -e 's/\A  \* \n//' debian/changelog
perl -pi'' -e 's/\A  \[ 127\.0\.0\.1 \]\n//' debian/changelog
perl -pi'' -e 's/\A  \[ amnesia \]\n//' debian/changelog
perl -pi'' -e 's/\A  \[ anonym \]\n//' debian/changelog
perl -pi'' -e 's/\A  \[ bertagaz \]\n//' debian/changelog
perl -pi'' -e 's/\A  \[ BitingBird \]\n//' debian/changelog
perl -pi'' -e 's/\A  \[ intrigeri \]\n//' debian/changelog
perl -pi'' -e 's/\A  \[ kytv \]\n//' debian/changelog
perl -pi'' -e 's/\A  \[ sajolida \]\n//' debian/changelog
perl -pi'' -e 's/\A  \[ T\(A\)ILS developers \]\n//' debian/changelog
perl -pi'' -e 's/\A  \[ Tails developers \]\n//' debian/changelog
perl -pi'' -e 's/\A  \[ Tails \]\n//' debian/changelog
perl -pi'' -e 's/\A  \* Added a comment\n//' debian/changelog
perl -pi'' -e 's/\A  \* Added a comment:.*\n//' debian/changelog
perl -pi'' -e 's/\A  \* Remove spam\.\n//' debian/changelog
perl -pi'' -e 's/\A  \* todo\+\+\n//i' debian/changelog
perl -pi'' -e 's/\A  \* todo--\n//i' debian/changelog
perl -pi'' -e 's/\A  \* TODO update[.]?\n//i' debian/changelog
perl -pi'' -e 's/\A  \* Update ticket[.]?\n//i' debian/changelog
perl -pi'' -e 's/\A  \* Now pending[.]?\n//i' debian/changelog
perl -pi'' -e 's/\A  \* Done[.]?\n//i' debian/changelog
perl -pi'' -e 's/\A  \* Upcoming release\n//' debian/changelog

# commit and tag the release
# if [ "${SNAPSHOT}" = no ]; then
#    echo "Commit'ing debian/changelog..."
#    git commit -m "releasing version ${NEW_VERSION}" debian/changelog \
#       || fatal "failed to commit debian/changelog"
#    echo "Tagging new version..."
#    git tag -u "${AMNESIA_DEV_KEYID}" -m "tagging version ${NEW_VERSION}" "${NEW_VERSION}"
# fi

echo "done."
